package com.rockcent.wechat.localController;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.utils.RBeanUtils;
import com.rockcent.mall.lib.dto.TransactionDto;
import com.rockcent.wechat.Routes;
import com.rockcent.wechat.helper.WechatHelper;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderQueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by DK on 16/12/8.
 */
@RestController
@RequestMapping(value = Routes.API_LOCAL_PREFIX)
public class MallBalanceAccountController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(MallBalanceAccountController.class);

    @Autowired
    private WechatHelper wechatHelper;

    @RequestMapping(value = Routes.MALL_WECHAT_ORDER_QUERY, method = RequestMethod.POST)
    public JsonResult wechatOrderQuery(HttpServletRequest request, @RequestHeader String domain, @RequestParam String transactionId, @RequestParam String orderNum) {
        RestDoing doing = jsonResult -> {
            WxMpService wxMpService = wechatHelper.getWxMpService(domain);
            WxPayOrderQueryResult queryResult =  wxMpService.getPayService().queryOrder(transactionId, orderNum);
            TransactionDto transaction = new TransactionDto();
            RBeanUtils.copyProperties(queryResult, transaction);
            jsonResult.data = transaction;
        };
        return doing.go(request, log);
    }
}
